package wangjg.game.android;

import wangjg.game.layer.GameCanvas;
import wangjg.game.layer.Graphics;
import wangjg.game.layer.Image;
import wangjg.xuexiban.util.ResoureUtil;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;

public class AndroidCanvas implements GameCanvas, Graphics {
	private SurfaceHolder holder;
	private Canvas canvas;
	private Paint paint;
	
	public AndroidCanvas(SurfaceHolder holder) {
		this.holder = holder;
		this.paint = new Paint();
	}

	@Override
	public Graphics getGraphics() {
		if(this.canvas!=null){
			this.flushGraphics();
		}
		return this;
	}

	@Override
	public void flushGraphics() {
		if(this.canvas!=null){
			holder.unlockCanvasAndPost(this.canvas);
			this.canvas = null;
		}
	}
	
	public Canvas getCanvas(){
		if(this.canvas==null){
			this.canvas = holder.lockCanvas();
		}
		return this.canvas;
	}
	
    public static final int TRANS_NONE = 0;
    public static final int TRANS_ROT90 = 5;
    public static final int TRANS_ROT180 = 3;
    public static final int TRANS_ROT270 = 6;
    public static final int TRANS_MIRROR = 2;
    public static final int TRANS_MIRROR_ROT90 = 7;
    public static final int TRANS_MIRROR_ROT180 = 1;
    public static final int TRANS_MIRROR_ROT270 = 4;
    
    public static final int TRANS_SCALE = 11;

	public void drawRegion(Image image_src, int x_src, int y_src, int width,
			int height, int transform, int x_dest, int y_dest, int anchor) {

		if ((anchor & VCENTER) != 0) {
			y_dest -= height / 2;
		} else if ((anchor & BOTTOM) != 0) {
			y_dest -= height;
		}
		if ((anchor & RIGHT) != 0) {
			x_dest -= width;
		} else if ((anchor & HCENTER) != 0) {
			x_dest -= width / 2;
		}

		Canvas g = this.getCanvas();
		g.save();

		g.clipRect(x_dest, y_dest, x_dest + width, y_dest + height);

		Matrix mMatrix = new Matrix();
		Matrix temp = new Matrix();

		float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1 };
		temp.setValues(mirrorY);

		switch (transform) {
		case TRANS_NONE:
			break;
		case TRANS_ROT90:
			mMatrix.setRotate(90, width / 2, height / 2);
			break;
		case TRANS_ROT180:
			mMatrix.setRotate(180, width / 2, height / 2);
			break;
		case TRANS_ROT270:
			mMatrix.setRotate(270, width / 2, height / 2);
			break;
		case TRANS_MIRROR:
			mMatrix.postConcat(temp);
			break;
		case TRANS_MIRROR_ROT90:
			mMatrix.postConcat(temp);
			mMatrix.setRotate(90, width / 2, height / 2);
			break;
		case TRANS_MIRROR_ROT180:
			mMatrix.postConcat(temp);
			mMatrix.setRotate(180, width / 2, height / 2);
			break;
		case TRANS_MIRROR_ROT270:
			mMatrix.postConcat(temp);
			mMatrix.setRotate(270, width / 2, height / 2);
			break;
		case TRANS_SCALE:
			int srcWidth = image_src.getWidth();
			int srcHeight = image_src.getHeight();
			Matrix matrix = ResoureUtil.getZoomMatrix(srcWidth, srcHeight, width, height);
			matrix.postTranslate(x_dest, y_dest);
			g.drawBitmap((Bitmap) image_src.getBitmap(), matrix, null);
			g.restore();
			if(true)return;
			
			mMatrix.postConcat(temp);
			float scaleWidth = ((float) width) / srcWidth;
	        float scaleHeight = ((float) height) / srcHeight;
	        mMatrix.postScale(scaleWidth, scaleHeight);
			break;
		}

		/*
		 * 这里的偏移要注意减去x_src和y_src 并且调用restore()恢复剪裁区
		 */
		mMatrix.setTranslate(x_dest - x_src, y_dest - y_src);
		g.drawBitmap((Bitmap) image_src.getBitmap(), mMatrix, null);
		g.restore();
	}
	
	@Override
	public void drawRect(int x, int y, int w, int h){
		paint.setStyle(Paint.Style.FILL);
		Canvas g = this.getCanvas();
		g.drawRect(x, y, x+w, y+h, paint);
	}
	
	@Override
	public void drawStrokeRect(int x, int y, int w, int h){
		this.paint.setStyle(Paint.Style.STROKE);
		Canvas g = this.getCanvas();
		g.drawRect(x, y, x+w, y+h, paint);
	}
	
	@Override
    public void drawText(String text, float x, float y) {
    	Canvas g = this.getCanvas();
    	g.drawText(text, x, y, paint);
    }
	
	@Override
	public void setColor(int color){
		paint.setColor(color);
	}
	
}
